package com.hundsun.data.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.hundsun.data.core.DynamicRoutingDataSource;
import com.hundsun.data.enums.DataSourceKey;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author pw
 * @date 2024年06月11日
 * @describe
 **/
@Configuration
public class DataSourceBeanConfig {
    /**
     * 配置默认数据源
     *
     * @return
     */
    @Bean(name = "dmDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dm")
    public DataSource dataSourceMaster() {
        return new DruidDataSource();
    }

    @Bean(name = "tdDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.td")
    public DataSource dataSourceStorage() {
        return new DruidDataSource();
    }

    @Bean(name = "oracleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.oracle")
    public DataSource dataSourcePay() {
        return new DruidDataSource();
    }

    /**
     * 设置多数据源，配置动态路由数据源
     *
     * @return
     */
    @Bean("dynamicDataSource")
    public DataSource dynamicDataSource(@Qualifier("dmDataSource") DataSource dmDataSource,
                                        @Qualifier("tdDataSource") DataSource tdDataSource,
                                        @Qualifier("oracleDataSource") DataSource oracleDataSource) {
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put(DataSourceKey.DM.name(), dmDataSource);
        dataSourceMap.put(DataSourceKey.TD.name(), tdDataSource);
        dataSourceMap.put(DataSourceKey.ORACLE.name(), oracleDataSource);
        //设置默认
        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
        dynamicRoutingDataSource.setDefaultTargetDataSource(dmDataSource);
        dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);
        return dynamicRoutingDataSource;
    }

    /**
     * 设置会话工厂
     *
     * @return
     * @throws Exception
     */
	@Bean(name="sqlSessionFactory")
	public SqlSessionFactoryBean sqlSessionFactoryBean(
			@Qualifier("dynamicDataSource") DataSource dataSource) {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		return sqlSessionFactoryBean;
	}

    /*@Bean(name="sqlSessionFactory")
    @Primary
    public SqlSessionFactory mybatisSqlSessionFactoryBean (
            @Qualifier("dynamicDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }*/

    /**
     * 配置事务管理器
     * @return
     */
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("dmDataSource") DataSource dmDataSource,
                                                         @Qualifier("tdDataSource") DataSource tdDataSource,
                                                         @Qualifier("oracleDataSource") DataSource oracleDataSource) {
        return new DataSourceTransactionManager(dynamicDataSource(dmDataSource, tdDataSource, oracleDataSource));
    }

}
